package com.shopping.runbackground.coupons.controller;

import com.shopping.entity.PageResult;
import com.shopping.entity.Result;
import com.shopping.entity.StatusCode;
import com.shopping.runbackground.coupons.api.pojo.CommonCoupons;
import com.shopping.runbackground.coupons.service.CommonCouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/commoncoupons")
public class CommonCouponsController {
    @Autowired
    private CommonCouponsService commonCouponsService;

    @GetMapping("/findall")
    public Result<CommonCoupons> findall(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize){
        try {
            PageResult<CommonCoupons> list = commonCouponsService.findall(currentPage, pageSize);
            return new Result<>(true,StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"查询失败");
        }


    }

    @PostMapping("/insert")
    public Result insert(@RequestBody CommonCoupons commonCoupons){
        try {
            commonCouponsService.insert(commonCoupons);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加失败");
        }

    }
    @PostMapping("/update")
    public Result update(@RequestBody CommonCoupons commonCoupons){
        try {
            commonCouponsService.update(commonCoupons);
            return new Result(true, StatusCode.OK,"更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"更新失败");
        }
    }
    @GetMapping("/delete")
    public Result delete(@RequestParam("ccId")Integer ccId){
        try {
            commonCouponsService.delete(ccId);
            return new Result(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }
    @GetMapping("/findbynameordate")
    public Result findbynameordate(@RequestBody Map map){
        //@RequestParam("cc_name")String cc_name, @RequestParam("starttime")Date startime,@RequestParam("endtime")Date endtime,@RequestParam("currentPage") Integer currentPage,@RequestParam("pageSize") Integer pageSize)
        try {
            PageResult<CommonCoupons> list = commonCouponsService.findbynameordate(map);
            return new Result<>(true,StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"查询失败");
        }
    }
}
